<template>
  <div class="app-container">
    <div class="head-container">
      <div style="display: flex;">
        <!-- 搜索 -->
        <label class="el-form-item-label">WMS任务号</label>
        <el-input v-model.trim="query.wmsTaskNo" clearable placeholder="WMS任务号" style="width: 185px;" class="filter-item" />
        <label class="el-form-item-label">堆垛机编号</label>
        <el-input v-model.trim="query.toDeviceCode" clearable placeholder="堆垛机编号" style="width: 185px;" class="filter-item" />
        <label class="el-form-item-label">优先级</label>
        <el-input v-model.trim="query.priority" clearable placeholder="优先级" style="width: 185px;" class="filter-item" />
        <label class="el-form-item-label">堆垛机任务状态</label>
        <el-select v-model="query.taskStatus" class="filter-item" clearable filterable placeholder="请选择" style="width: 185px;">
          <el-option
            v-for="item in dict.stacker_task_status"
            :key="item.id"
            :label="item.label"
            :value="item.value"
          />
        </el-select>
        <label class="el-form-item-label">堆垛机任务类型</label>
        <el-select v-model="query.taskType" class="filter-item" clearable filterable placeholder="请选择" style="width: 185px;">
          <el-option
            v-for="item in dict.stacker_task_type"
            :key="item.id"
            :label="item.label"
            :value="item.value"
          />
        </el-select>
        <div style="margin-left: 5px;">
          <el-button type="success" size="mini" class="filter-item" icon="el-icon-search">搜索</el-button>
          <el-button type="warning" size="mini" class="filter-item" icon="el-icon-refresh-right">重置</el-button>
        </div>
      </div>
      <!-- 列表渲染 -->
      <el-table ref="table" v-loading="loading" :data="list" size="small" style="width: 100%;">
        <!-- <el-table-column type="selection" width="55" /> -->
        <el-table-column prop="wmsTaskNo" label="WMS任务号" />
        <el-table-column prop="taskNo" label="堆垛机任务号" />
        <el-table-column prop="toDeviceCode" label="堆垛机号" />
        <el-table-column prop="taskType" label="类型">
          <template slot-scope="scope">
            {{ dict.label.stacker_task_type[scope.row.taskType] }}
          </template>
        </el-table-column>
        <el-table-column prop="endCol" label="结束列" />
        <el-table-column prop="endLevel" label="结束层" />
        <el-table-column prop="endRow" label="结束排" />
        <el-table-column prop="startCol" label="开始列" />
        <el-table-column prop="startLevel" label="开始层" />
        <el-table-column prop="startRow" label="开始排" />
        <el-table-column prop="priority" label="优先级" />
        <el-table-column prop="taskStatus" label="任务状态">
          <template slot-scope="scope">
            {{ dict.label.stacker_task_status[scope.row.taskStatus] }}
          </template>
        </el-table-column>
        <el-table-column prop="creationTime" label="创建时间" />
        <el-table-column label="操作" width="150px" align="center">
          <template slot-scope="scope">
            <el-popconfirm
              confirm-button-text="确定"
              cancel-button-text="取消"
              icon="el-icon-info"
              icon-color="red"
              title="点击此按钮,将强制结束该任务,是否继续?"
              @confirm="forceEnd(scope.row, scope.$index)"
            >
              <el-button slot="reference" icon="el-icon-switch-button" type="danger" size="mini" :loading="btn.force === scope.$index">强制结束</el-button>
            </el-popconfirm>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页 -->
      <el-pagination
        :page-size.sync="query.page.size"
        :total="total"
        :current-page.sync="query.page.current"
        style="margin-top: 8px;"
        layout="total, prev, pager, next, sizes"
        @size-change="sizeChangeHandler($event)"
        @current-change="pageChangeHandler"
      />
    </div>
  </div>
</template>

<script>
import { getStackerCraneTaskListAPI, forceStackerEndAPI } from '@/api/work/wcsMission'
export default {
  name: 'StackerCraneTask',
  dicts: ['stacker_task_status', 'stacker_task_type'],
  data() {
    return {
      query: {
        wmsTaskNo: '',
        isWrapped: '',
        isStrapping: '',
        status: '',
        page: {
          current: 1,
          size: 20
        }
      },
      total: 0,
      loading: false,
      list: [
        // {
        //   creationTime: '2025-05-14 23:03:38',
        //   endCol: '2',
        //   endLevel: '2',
        //   endRow: 'A2',
        //   priority: 1,
        //   startCol: '1',
        //   startLevel: '1',
        //   startRow: 'A1',
        //   taskNo: 1,
        //   taskStatus: 0,
        //   taskType: 1,
        //   toDeviceCode: 101,
        //   wmsTaskNo: 'WMS12345'
        // }
      ],
      btn: {
        force: ''
      }
    }
  },
  mounted() {
    this.toQuery()
  },
  methods: {
    toQuery() {
      this.query.page.current = 1
      this.getTable()
    },
    async getTable() {
      try {
        this.loading = true
        const res = await getStackerCraneTaskListAPI(this.query)
        if (res.result === 'OK') {
          this.list = res.data.records
          this.total = res.data.total
        }
      } finally {
        this.loading = false
      }
    },
    sizeChangeHandler(e) {
      this.query.page.size = e
      this.query.page.current = 1
      this.getTable()
    },
    pageChangeHandler(e) {
      this.query.page.current = e
      this.getTable()
    },
    async forceEnd(row, index) {
      try {
        this.btn.force = index
        const res = await forceStackerEndAPI({
          taskNo: row.taskNo,
          wmsTaskNo: row.wmsTaskNo
        })
        if (res.result === 'OK') {
          this.$message.success('强制结束成功')
          this.crud.toQuery()
        } else {
          this.$message.warning(res.data)
        }
      } finally {
        this.btn.force = ''
      }
    }
  }
}
</script>

<style scoped lang="scss">
</style>
